<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="发票类型" prop="type">
        <el-select
          v-model="queryParams.type"
          filterable
          placeholder="请选择发票类型"
        >
          <el-option
            v-for="item in companyAllList"
            :key="item.code"
            :label="item.title"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发票号码" prop="fphm">
        <el-input
          v-model="queryParams.fphm"
          placeholder="请输入发票号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销方名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入销方名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报销人" prop="bxr">
        <el-input
          v-model="queryParams.bxr"
          placeholder="请输入报销人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开票日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleAdd"
          :disabled="multiple"
          >批量打印</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          >导入</el-button
        >
      </el-col>
    </el-row>

    <el-table
      style="margin-top: 20px"
      v-loading="loading"
      :data="invoiceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发票类型" align="center" prop="type" />
      <el-table-column label="发票号码" align="center" prop="fphm" />
      <el-table-column label="销方名称" align="center" prop="mc2" />
      <el-table-column label="开票金额" align="center" prop="hjje" />
      <el-table-column label="开票日期" align="center" prop="kprq" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改商家管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <div style="display: flex; align-items: center; justify-content: center">
        <div
          style="
            display: flex;
            align-items: center;
            flex-direction: column;
            cursor: pointer;
          "
          :class="printType === 'A4' ? 'active' : ''"
          @click="printType = 'A4'"
        >
          <i class="el-icon-tickets" style="font-size: 120px"></i>
          <div>A4</div>
        </div>
        <div
          style="
            display: flex;
            align-items: center;
            flex-direction: column;
            margin-left: 40px;
            cursor: pointer;
          "
          :class="printType === 'A5' ? 'active' : ''"
          @click="printType = 'A5'"
        >
          <i class="el-icon-tickets" style="font-size: 150px"></i>
          <div>A5</div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".zip, .pdf"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        name="multipartFile"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" />
              是否更新已经存在的用户数据
            </div> -->
          <span>仅允许导入zip、pdf格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInvoice,
  getInvoice,
  delInvoice,
  addInvoice,
  updateInvoice,
} from "@/api/business/invoice";
import {
  listInvoiceClass,
  getInvoiceClass,
  delInvoiceClass,
  addInvoiceClass,
  updateInvoiceClass,
} from "@/api/business/invoiceClass";

import { getToken } from "@/utils/auth";

export default {
  name: "Invoice",
  data() {
    return {
      printType: "A4",
      dateRange: [],
      companyAllList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 发票表格数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/business/invoice/setClass",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        nsrsbh1: null,
        nsrsbh2: null,
        title: null,
        type: null,
        fh: null,
        hjje: null,
        hjse: null,
        jshjxx: null,
        jshjdx: null,
        mmq: null,
        kprq: null,
        khhjzh1: null,
        mc1: null,
        dzdh1: null,
        khhjzh2: null,
        skr: null,
        kpr: null,
        mc2: null,
        dzdh2: null,
        fpdm: null,
        jqbh: null,
        fphm: null,
        jym: null,
        remark: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发票列表 */
    getList() {
      this.loading = true;
      listInvoiceClass(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((response) => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        nsrsbh1: null,
        nsrsbh2: null,
        title: null,
        type: null,
        fh: null,
        hjje: null,
        hjse: null,
        jshjxx: null,
        jshjdx: null,
        mmq: null,
        kprq: null,
        khhjzh1: null,
        mc1: null,
        dzdh1: null,
        khhjzh2: null,
        skr: null,
        kpr: null,
        mc2: null,
        dzdh2: null,
        fpdm: null,
        jqbh: null,
        fphm: null,
        jym: null,
        remark: null,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.dateRange = [];
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "打印设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInvoice(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.download(
        "business/invoiceClass/exportClass",
        {
          ids: this.ids.join(","),
          type: this.printType,
        },
        `报销记录_${new Date().getTime()}.xlsx`
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除发票编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInvoice(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/invoiceClass/exportClass",
        {
          ...this.queryParams,
        },
        `报销记录_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "business/invoiceClass/importTemplate",
        {},
        `收票排版_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style>
.app-container {
  padding: 20px 30px;
}
.active {
  color: #409eff;
}
</style>
